﻿using System;
using DotNETStack.Core.Data;
using DotNETStack.LinqToSqlDataProvider.Data;

namespace DotNETStack.LinqToSqlDataProvider
{
    public class DataProvider : IDataProvider
    {
        #region Private Fields

        private readonly DotNETStackDbDataContext _dataContext;
        private IQuestionRepository _questionRepository;
        private IUserRepository _userRepository;

        #endregion

        #region Constructors

        public DataProvider()
            : this(null)
        {
        }

        public DataProvider(String connectionString)
        {
            if (!String.IsNullOrEmpty(connectionString))
                _dataContext = new DotNETStackDbDataContext(connectionString);
            else
                _dataContext = new DotNETStackDbDataContext();
        }

        #endregion

        #region IDataProvider Members

        public IQuestionRepository QuestionRepository
        {
            get
            {
                if (_questionRepository == null)
                {
                    _questionRepository = new QuestionRepository(_dataContext);
                }
                return _questionRepository;
            }
        }

        public IUserRepository UserRepository
        {
            get
            {
                if (_userRepository == null)
                {
                    _userRepository = new UserRepository(_dataContext);
                }
                return _userRepository;
            }
        }

        #endregion
    }
}